package com.cloud.rou.im.cluster.naming.node;

import com.cloud.rou.im.cluster.zk.ZookeeperClientFactory;
import org.apache.curator.framework.listen.Listenable;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;

/**
 * <p>
 *     订阅实例变化事件
 * </p>
 *
 * @author GaoShuangWen
 * @since 2019-11-13 21:17
 */
public class SubInstanceEvent {

    public void sub(InstanceTreeCacheListener listener){
        TreeCache treeCache = new TreeCache(ZookeeperClientFactory.getInstance().getZkCli(), "/im/instance");
        Listenable<TreeCacheListener> listenable = treeCache.getListenable();
        listenable.addListener(listener);
        try {
            treeCache.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
